<template>
	<view class="page">
		<scroll-view @scrolltolower="handleScrollToLower" class="remove_tabbar" scroll-y :lower-threshold="300">
			<u-tabs :list="list1" @click="changeTabs" lineColor='#FBE43F'
				:inactiveStyle="{ fontSize: '32rpx', transform: 'scale(1)',color: '#979797'  }"
				:activeStyle="{ color: '#000000',fontSize: '40rpx', }">
			</u-tabs>
			<!-- 校源情报 -->
			<ConferenceHall :tabList="tabList" :swiper="swiper" v-if="CurrentTabs == 0">
			</ConferenceHall>
			<!-- 互助跑腿 -->
			<MutualAidHall :swiper="swiper" :tabList="tabList" v-if="CurrentTabs == 1" @change="changeType">
			</MutualAidHall>
			<!-- 福利好物 -->
			<TribalWelfare :swiper="swiper" :tabList="tabList" v-if="CurrentTabs == 2"></TribalWelfare>
		</scroll-view>

		<!-- 底部tabbar -->
		<Tabbar :current-page="currentPage"></Tabbar>
		<pop :showPop="showPop" :popList='popList' @close='close'></pop>
	</view>
</template>

<script>
	import Tabbar from '@/compontents/Tabbar.vue'
	import ConferenceHall from '@/compontents/ConferenceHall.vue'
	import MutualAidHall from '@/compontents/MutualAidHall.vue'
	import TribalWelfare from '@/compontents/TribalWelfare.vue'
	import pop from '@/compontents/pop.vue'
	import {
		memberLogin,
		getMemberInfo,
		discussionList,
		listByPosition,
		getHelpOrderList,
		groupGoodsList
	} from '@/api.js'
	export default {
		components: {
			Tabbar,
			ConferenceHall,
			MutualAidHall,
			TribalWelfare,
			pop
		},

		data() {
			return {
				pageNo: 1,
				pageSize: 10,
				total: 0,
				loading: false,
				currentPage: 'home',
				list1: [{
					name: '校源情报',
				}, {
					name: '互助跑腿',
				}, {
					name: '福利好物'
				}],
				tabList: [],
				swiper: [

				],
				popList: [],
				CurrentTabs: 0,
				userInfo: {},
				// 这个是互助跑腿中的菜单索引
				type: 1,
				showPop: false,

			}
		},

		onLoad(options) {

			let that = this

			this.init()
			if (!sessionStorage.getItem('hasShownPopup')) {
				console.log('显示');
				that.showPop = true

			}

		},
		methods: {
			// 关闭弹窗
			close() {
				this.showPop = false
				sessionStorage.setItem('hasShownPopup', 'true');
			},
			// 互助跑腿切换tabs
			changeType(e) {
				console.log('互助跑腿切换分类', e);
				this.type = e
				this.reset()
			},
			// 重置分页数据
			reset() {
				this.pageNo = 1
				this.pageSize = 10
				this.total = 0
				this.tabList = []
				this.getList()

			},

			handleScrollToLower() {
				let pageNo = this.pageNo
				let pageSize = this.pageSize
				let total = this.total
				console.log('一共' + total + '条，当前已经加载' + pageSize * pageNo + '条');
				if (total <= pageNo * pageSize) {
					console.log('已经到底了');
					return
				}
				uni.showLoading({
					title: '加载中'
				});
				this.pageNo++;
				this.getList();
				// uni.hideLoading();
			},
			async getList() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				console.log('CurrentTabs', this.CurrentTabs);
				if (this.CurrentTabs == 0) {
					let res = await discussionList({
						pageNo: this.pageNo,
						pageSize: this.pageSize
					})
					this.tabList = this.tabList.concat(res.result.records)
					this.total = res.result.total
					console.log('11111');
					uni.hideLoading();
				} else if (this.CurrentTabs == 1) {
					let res = await getHelpOrderList({
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						type: this.type
					})
					this.tabList = this.tabList.concat(res.result.records)
					this.total = res.result.total
					console.log('2222');
					uni.hideLoading();
				} else if (this.CurrentTabs == 2) {
					let res = await groupGoodsList({
						pageNo: this.pageNo,
						pageSize: this.pageSize
					})
					this.tabList = this.tabList.concat(res.result.records)
					this.total = res.result.total
					console.log('3333');
					uni.hideLoading();
				}


			},
			changeTabs(e) {
				this.CurrentTabs = e.index
				this.getBanner()
				this.reset()
			},
			async init() {
				this.getPop()
				let that = this
				if (uni.getStorageSync('token')) {
					this.GetUser()
					this.getBanner()
					this.getList()
					return
				}
				const searchParams = new URLSearchParams(window.location.search);
				const code = searchParams.get('code');
				console.log('code', code);
				if (code) {
					let res = await memberLogin({
						code
					})
					console.log('登录返回结果', res);
					if (res.code === 200 && res.success) {
						const token = res.result.token;
						uni.setStorage({
							key: 'token',
							data: token,
							success: function() {
								that.GetUser()
								that.getBanner()
								that.getList()
							}
						});
					} else {
						console.error('登录失败，错误信息:', res.message);
					}
				} else {
					uni.showModal({
						title: '提醒',
						content: '为了您更好地体验，请先登录',
						success: function(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: "/pages/login/authorize"
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					return
				}

			},
			async getBanner() {
				let res = await listByPosition({
					position: this.CurrentTabs + 1
				})
				this.swiper = res.result
				console.log('轮播', res);
			},
			async getPop() {
				let res = await listByPosition({
					position: 4
				})
				this.popList = res.result
			},
			// 获取用户信息
			async GetUser() {
				let res = await getMemberInfo()
				console.log('res用户信息', res.result);
				this.userInfo = res.result
				uni.setStorage({
					key: 'userInfo',
					data: JSON.stringify(res.result),
				});
				if (!this.userInfo.schoolId || this.userInfo.schoolId == '') {
					uni.reLaunch({
						url: '/pages/login/chooseSchool'
					})
				}
			},
		},
	}
</script>

<style lang="scss" scoped>

</style>